import request from '@/utils/request'

export interface CustomerDTO {
  customerId?: string
  customerCode: string
  customerName: string
  customerType: 'ENTERPRISE' | 'INDIVIDUAL'
  categoryId?: string
  levelId?: string
  industry?: string
  legalPerson?: string
  registeredCapital?: number
  creditCode?: string
  taxNumber?: string
  phone?: string
  email?: string
  website?: string
  address?: string
  province?: string
  city?: string
  district?: string
  postalCode?: string
  bankName?: string
  bankAccount?: string
  ownerUserId?: string
  ownerDeptId?: string
  status?: number
  source?: string
  remark?: string
}

export interface CustomerCategoryDTO {
  categoryId?: string
  categoryCode: string
  categoryName: string
  parentId?: string
  sortOrder?: number
  status?: number
  remark?: string
}

export interface CustomerLevelDTO {
  levelId?: string
  levelCode: string
  levelName: string
  minAmount?: number
  maxAmount?: number
  discountRate?: number
  sortOrder?: number
  status?: number
  remark?: string
}

// 客户管理API
export function pageCustomer(params: {
  pageNum: number
  pageSize: number
  customerCode?: string
  customerName?: string
  customerType?: string
  categoryId?: string
  levelId?: string
  status?: string
  source?: string
  ownerUserId?: string
  startTime?: string
  endTime?: string
}) {
  return request({ url: '/crm/customer/page', method: 'get', params })
}

export function getCustomer(id: string) {
  return request({ url: `/crm/customer/${id}`, method: 'get' })
}

export function createCustomer(data: CustomerDTO) {
  return request({ url: '/crm/customer', method: 'post', data })
}

export function updateCustomer(id: string, data: CustomerDTO) {
  return request({ url: `/crm/customer/${String(id)}`, method: 'put', data })
}

export function deleteCustomer(id: string) {
  return request({ url: `/crm/customer/${String(id)}`, method: 'delete' })
}

export function deleteCustomerBatch(ids: string[]) {
  return request({ url: '/crm/customer/batch-delete', method: 'post', data: ids })
}

// 客户分类API
export function pageCustomerCategory(params: {
  pageNum: number
  pageSize: number
  categoryCode?: string
  categoryName?: string
  parentId?: string
  status?: string
}) {
  return request({ url: '/crm/customer/category/page', method: 'get', params })
}

export function getCustomerCategoryTree(status?: string) {
  return request({ url: '/crm/customer/category/tree', method: 'get', params: { status } })
}

export function getCustomerCategory(id: string) {
  return request({ url: `/crm/customer/category/${id}`, method: 'get' })
}

export function createCustomerCategory(data: CustomerCategoryDTO) {
  return request({ url: '/crm/customer/category', method: 'post', data })
}

export function updateCustomerCategory(id: string, data: CustomerCategoryDTO) {
  return request({ url: `/crm/customer/category/${String(id)}`, method: 'put', data })
}

export function deleteCustomerCategory(id: string) {
  return request({ url: `/crm/customer/category/${String(id)}`, method: 'delete' })
}

export function deleteCustomerCategoryBatch(ids: string[]) {
  return request({ url: '/crm/customer/category/batch-delete', method: 'post', data: ids })
}

// 客户等级API
export function pageCustomerLevel(params: {
  pageNum: number
  pageSize: number
  levelCode?: string
  levelName?: string
  status?: string
}) {
  return request({ url: '/crm/customer/level/page', method: 'get', params })
}

export function listCustomerLevel(status?: string) {
  return request({ url: '/crm/customer/level/list', method: 'get', params: { status } })
}

export function getCustomerLevel(id: string) {
  return request({ url: `/crm/customer/level/${id}`, method: 'get' })
}

export function createCustomerLevel(data: CustomerLevelDTO) {
  return request({ url: '/crm/customer/level', method: 'post', data })
}

export function updateCustomerLevel(id: string, data: CustomerLevelDTO) {
  return request({ url: `/crm/customer/level/${String(id)}`, method: 'put', data })
}

export function deleteCustomerLevel(id: string) {
  return request({ url: `/crm/customer/level/${String(id)}`, method: 'delete' })
}

export function deleteCustomerLevelBatch(ids: string[]) {
  return request({ url: '/crm/customer/level/batch-delete', method: 'post', data: ids })
}

